package org.ptkparty.client.activity;

import java.util.List;

import org.ptkparty.client.ClientFactory;
import org.ptkparty.client.Context;
import org.ptkparty.client.place.ProfilPlace;
import org.ptkparty.client.view.games.ResultsView;
import org.ptkparty.shared.dto.ResultDto;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ResultsActivity implements Activity {

	private ClientFactory clientFactory;
	private ResultsView view;

	public ResultsActivity(ClientFactory clientFactory, String currentChampionship) {
		this.clientFactory = clientFactory;
		Context.get().setCurrentChampionship(currentChampionship);
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view = clientFactory.getResultsView();
		view.setActivity(this);
		panel.setWidget(view);

		clientFactory.getPtkService().getResults(Context.get().getCurrentChampionship(), new AsyncCallback<List<ResultDto>>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(List<ResultDto> result) {
				view.loadData(result);
			}

		});
	}

	@Override
	public String mayStop() {
		return null;
	}

	@Override
	public void onCancel() {
	}

	@Override
	public void onStop() {
	}

	public void playerSelected(String userId) {
		clientFactory.getPlaceController().goTo(new ProfilPlace(Context.get().getCurrentChampionship(), userId));
	}

}
